
package com.hoshiicloud.user.controller.company;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hoshiicloud.common.rpc.Response;
import com.hoshiicloud.common.utils.BeanUtils;
import com.hoshiicloud.user.entity.company.BusinessAreas;
import com.hoshiicloud.user.entity.organization.ComOrganizationCategory;
import com.hoshiicloud.user.param.company.BusinessAreasAddParam;
import com.hoshiicloud.user.param.company.BusinessAreasQueryParam;
import com.hoshiicloud.user.param.company.BusinessAreasUpdateParam;
import com.hoshiicloud.user.param.organization.ComOrganizationCategoryAddParam;
import com.hoshiicloud.user.param.organization.ComOrganizationCategoryQueryParam;
import com.hoshiicloud.user.param.organization.ComOrganizationCategoryUpdateParam;
import com.hoshiicloud.user.service.company.IBusinessAreasService;
import com.hoshiicloud.user.service.organization.ComOrganizationCategoryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import io.swagger.annotations.ApiSort;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;


/**
 * <p>
 * 经营领域business_areas控制器
 * </p>
 *
 * @author campchen
 * @since 2019-08-21
 */

@Slf4j
@RestController
@RequestMapping("/organization/business-areas")
@Api(value = "SAAS租户供应商经营领域", tags = {"SAAS租户供应商经营领域"})
public class BusinessAreasController {
    @Autowired
    private IBusinessAreasService businessAreasService;


    /**
     * @param param
     * @return com.hoshiicloud.common.rpc.Response
     * @description: 分页查询经营领域business_areas
     */
    @ApiOperation(value = "分页查询经营领域business_areas")
    @PostMapping("/getPage")
    public Response page(@RequestBody BusinessAreasQueryParam param) {
        try {
            QueryWrapper<BusinessAreas> queryWrapper = new QueryWrapper<BusinessAreas>();
            queryWrapper.lambda()
                    .eq((param.getParentId() != null && param.getParentId() != 0), BusinessAreas::getParentId, param.getParentId())
                    .eq(StringUtils.isNotBlank(param.getName()), BusinessAreas::getName, param.getName());
            Page<BusinessAreas> page = new Page<>(Long.parseLong(param.getPageNumber()), Long.parseLong(param.getPageSize()));
            String key = param.toString() + ",Page{pageNumber=" + param.getPageNumber() + ",pageSize=" + param.getPageSize() + "}";
            return Response.data(businessAreasService.getPage(page, queryWrapper, key));
        } catch (Exception e) {
            log.error("分页查询经营领域business_areas异常", e);
            return Response.failed("分页查询经营领域business_areas异常");
        }
    }

    /**
     * @param id
     * @return com.hoshiicloud.common.rpc.Response
     * @description: 根据id查询经营领域business_areas
     */
    @GetMapping("/{id}")
    @ApiOperation(value = "根据id查询经营领域business_areas")
    @ApiParam(name = "id", value = "主键ID", required = true)
    public Response queryById(@PathVariable("id") Long id) {
        return Response.data(businessAreasService.getById(id));
    }


    /**
     * @param param
     * @return com.hoshiicloud.common.rpc.Response
     * @description: 新增
     */
    @PostMapping("/save")
    @ApiOperation(value = "新增经营领域business_areas")
    public Response save(@RequestBody @Valid BusinessAreasAddParam param) {
        BusinessAreas data;
        try {
            data = BeanUtils.covertType(param, BusinessAreas.class);
        } catch (Exception e) {
            log.error("新增机构经营领域business_areas", e);
            return Response.failed("新增经营领域business_areas Bean转换异常");
        }
        return Response.data(businessAreasService.save(data));
    }


    /**
     * @param param
     * @return com.hoshiicloud.common.rpc.Response
     * @description: 修改经营领域business_areas
     */
    @PutMapping("/update")
    @ApiOperation(value = "修改经营领域business_areas")
    public Response update(@RequestBody @Valid BusinessAreasUpdateParam param) {
        BusinessAreas data;
        try {
            data = BeanUtils.covertType(param, BusinessAreas.class);
        } catch (Exception e) {
            log.error("修改经营领域business_areas Bean转换异常", e);
            return Response.failed("修改经营领域business_areas Bean转换异常");
        }
        return Response.data(businessAreasService.updateById(data));
    }

}

